# coding=utf-8
import requests
import operator
from bs4 import BeautifulSoup

# 爬虫结构：模拟登陆教学管理系统来抓取信息并和培养方案比对取得数据

# get_login_cookies(password_user,username_user)，传入用户名和密码，返回授权后的 cookies
def get_login_cookies(password_user,username_user):
	# 第一步、进入登陆页面，获取cookie
	headers_index = {
	'Host': 'jwgl.yibinu.edu.cn',
	'Origin': 'http://jwgl.yibinu.edu.cn',
	'Upgrade-Insecure-Requests': '1',
	'DNT': '1',
	'Content-Type': 'application/x-www-form-urlencoded',
	'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.87 Safari/537.36',
	'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
	'Accept-Encoding': 'gzip, deflate',
	'Accept-Language': 'zh-CN,zh;q=0.9',
	}
	r = requests.get('http://jwgl.yibinu.edu.cn/web/web/web/index.asp?xt_fb=LC0xMTU3MywtMTk5NzgsLTExODY1LC0xMTA3OA', headers=headers_index) 
	index_cookies = r.cookies
	# 第二步、进入check页面，获取cookie
	check_url = 'http://jwgl.yibinu.edu.cn/jiaoshi/bangong/check.asp'
	data = {
	'user' : username_user,
	'pwd' : password_user
	}
	headers = {
	'Host': 'jwgl.yibinu.edu.cn',
	'Connection': 'keep-alive',
	'Content-Length': '33',
	'Cache-Control': 'max-age=0',
	'Origin': 'http://jwgl.yibinu.edu.cn',
	'Upgrade-Insecure-Requests': '1',
	'DNT': '1',
	'Content-Type': 'application/x-www-form-urlencoded',
	'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.87 Safari/537.36',
	'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
	'Referer': 'http://jwgl.yibinu.edu.cn/web/web/web/index.asp',
	'Accept-Encoding': 'gzip, deflate',
	'Accept-Language': 'zh-CN,zh;q=0.9',
	}
	r = requests.post(check_url, data=data, headers=headers, cookies = index_cookies) 
	checked_cookies = r.cookies
	checked_headers = {
	'Host': 'jwgl.yibinu.edu.cn',
	'Connection': 'keep-alive',
	'Cache-Control': 'max-age=0',
	'Upgrade-Insecure-Requests': '1',
	'DNT': '1',
	'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.87 Safari/537.36',
	'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
	'Referer': 'http://jwgl.yibinu.edu.cn/web/web/web/index.asp',
	'Accept-Encoding': 'gzip, deflate',
	'Accept-Language': 'zh-CN,zh;q=0.9',
	}
	# 第三步、进入index1页面，获取cookie
	index1_url = 'http://jwgl.yibinu.edu.cn/jiaoshi/bangong/main/index1.asp'
	r = requests.get(index1_url, data = data, headers=checked_headers, cookies = checked_cookies) 
	logined_cookies = r.cookies
	return logined_cookies

# get_self_info(logined_cookies)，传入授权后的 cookie ，获取个人信息(姓名，学号)
def get_self_info(logined_cookies):
	self_info_headers = {
	'Host': 'jwgl.yibinu.edu.cn',
	'Connection': 'keep-alive',
	'Cache-Control': 'max-age=0',
	'Upgrade-Insecure-Requests': '1',
	'DNT': '1',
	'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.87 Safari/537.36',
	'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
	'Referer': 'http://jwgl.yibinu.edu.cn/web/web/web/index.asp',
	'Accept-Encoding': 'gzip, deflate',
	'Accept-Language': 'zh-CN,zh;q=0.9',
	'Referer': 'http://jwgl.yibinu.edu.cn/jiaoshi/xueji/xue/stuInfoUp.asp'
	}
	self_info_url = 'http://jwgl.yibinu.edu.cn/jiaoshi/xueji/xue/stuInfoUp.asp'
	r = requests.get(self_info_url, headers=self_info_headers, cookies = logined_cookies) 
	content = r.content.decode('gbk')
	soup = BeautifulSoup(content,'html.parser')
	need_tag_table = soup.find_all('table',attrs={"id":"Table1"})[0]
	need_tag_tr = need_tag_table.find_all('tr')[1]
	xuehao = need_tag_tr.find_all('td')[1].get_text()
	xingming = need_tag_tr.find_all('td')[-1].get_text()
	self_info_list = []
	self_info_list.append(xingming)
	self_info_list.append(xuehao)
	return self_info_list


# get_learned_cources(logined_cookies)，传入已授权的 cookie 获取所有已修课程
def get_learned_cources(logined_cookies):

	learned_cources_headers = {
	'Host': 'jwgl.yibinu.edu.cn',
	'Connection': 'keep-alive',
	'Cache-Control': 'max-age=0',
	'Upgrade-Insecure-Requests': '1',
	'DNT': '1',
	'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.87 Safari/537.36',
	'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
	'Referer': 'http://jwgl.yibinu.edu.cn/web/web/web/index.asp',
	'Accept-Encoding': 'gzip, deflate',
	'Accept-Language': 'zh-CN,zh;q=0.9',
	'Referer': 'http://jwgl.yibinu.edu.cn/jiaoshi/xueji/xue/stuInfoUp.asp'
	}
	learned_course_url = 'http://jwgl.yibinu.edu.cn/jiaoshi/xslm/cj/search.asp'
	all_cources_info_list = []
	r = requests.post(learned_course_url, headers=learned_cources_headers, cookies = logined_cookies) 
	content = r.content.decode('gbk')
	soup = BeautifulSoup(content,'html.parser')
	need_tag_table = soup.find_all('table')[-2]
	need_tag_trs_g_body_2 = need_tag_table.find_all('tr',attrs={"class":"g_body_2"})
	need_tag_trs_g_body_1 = need_tag_table.find_all('tr',attrs={"class":"g_body_1"})
	for i in range(len(need_tag_trs_g_body_2)):
		a_cource = []
		need_tag_tr = need_tag_trs_g_body_2[i]
		need_tag_tds = need_tag_tr.find_all('td')
		cource_term = need_tag_tds[1].get_text()
		cource_teacher = need_tag_tds[2].get_text()
		cource_name = need_tag_tds[3].get_text()
		cource_type = need_tag_tds[4].get_text()
		cource_credit = need_tag_tds[5].get_text()
		cource_score = need_tag_tds[6].get_text()
		a_cource.append(cource_name)
		a_cource.append(cource_type)
		a_cource.append(cource_credit)
		a_cource.append(cource_teacher)
		a_cource.append(cource_term)
		a_cource.append(cource_score)
		all_cources_info_list.append(a_cource)
	for i in range(len(need_tag_trs_g_body_1)):
		a_cource = []
		need_tag_tr = need_tag_trs_g_body_1[i]
		need_tag_tds = need_tag_tr.find_all('td')
		cource_term = need_tag_tds[1].get_text()
		cource_teacher = need_tag_tds[2].get_text()
		cource_name = need_tag_tds[3].get_text()
		cource_type = need_tag_tds[4].get_text()
		cource_credit = need_tag_tds[5].get_text()
		cource_score = need_tag_tds[6].get_text()
		a_cource.append(cource_name)
		a_cource.append(cource_type)
		a_cource.append(cource_credit)
		a_cource.append(cource_teacher)
		a_cource.append(cource_term)
		a_cource.append(cource_score)
		all_cources_info_list.append(a_cource)
	return all_cources_info_list

def get_analysis(all_cources_info_list):
	# 1. 按 创新思维能力课程、通识课程、专业核心课程、应用创新课程 来分割列表
	cxswnlkc = []
	credit_cxswnlkc = 0
	tskc = []
	credit_tskc = 0
	zyhxkc = []
	credit_zyhxkc = 0
	yycxkc = []
	credit_yycxkc = 0

	# 校级创新思维能力课程
	xjcxswnlkc = []
	credit_xjcxswnlkc = 0
	# 通识校级B课程
	tsxjb = []
	credit_tsxjb = 0

	# 未被培养方案包含的课程
	un_covered_courses = []

	# 不及格的课程
	un_pass_courses = []

	# 培养方案以列表形式放在此处
	base_need_cxswnlkc=['创新性思维与方法','社会科学思维']
	base_need_tskc=['思想道德修养与法律基础','中国近现代史纲要B','毛泽东思想和中国特色社会主义理论体系概论','马克思主义基本原理概论','大学外语A1','大学外语A2','大学外语A3','大学外语A4','体育1','体育2','体育3','体育4','计算机基础（文）B','计算机基础（文）实验E','自然科学概论（文）','形势与政策1','形势与政策2','创业基础课','就业指导课 ']
	base_need_zyhxkc=['社会工作导论','人类行为与社会环境','社会调查研究方法','应用心理学','个案工作','小组工作','社区工作','社会福利与保障','社会政策与法规']
	base_need_yycxkc=['社会学概论','军事理论与训练','当代中国社会问题','社会工作职场礼仪','妇女社会工作','老年社会工作','青少年社会工作','社会工作项目申报','社会统计学与SPSS','学校社会工作','家庭社会工作','社会工作活动设计','社会工作项目评估','社会工作项目实施','专业实习','社会工作创新实践','民政工作概论 ','社会工作考研与职业考试 ','实用市场调查 ','文献检索与论文写作 ','物业管理概论','西方社会学理论','婚姻与家庭继承法','劳动合同法','公共事业管理学','社会组织管理','社会工作前沿','毕业实习','毕业论文']
	base_need_xjcxswnlkc=['公共交际与职场礼仪','国学经典与华夏文化','影视鉴赏与艺术人生','创意写作','国学智慧与创意人生','创意思维与演讲口才','跨文化交际实践能力培养','《圣经》与西方文化','法制理念与实践','实用社会调查方法','人与自我','教育启迪人生','生命全程心理发展与心理健康','微生物与人类健康','近现代科技的创造思维与革新','物理学与世界进步','歌唱艺术欣赏与技法','中国美术十八讲']
	base_need_tsxjb=['《圣经》与西方文化（通识教育中标课程）','C语言程序设计','VB程序设计','参与式教学理论与方法','初级日语','当代世界经济与政治','法治理念与实践（通识教育中标课程）','犯罪与刑罚','公共关系学','公共管理前沿与实践分析','公务员考试(网络课程)','公务员应试基础','环境美学','环境生物学','婚姻家庭继承法','基础教育改革动向与热点透视','健康与保健','教师礼仪','金融学','金融学基础','经济学基础知识及热点问题普及与解析','劳动法与大学生就业','历史表象与思维','粮食危机与食品安全','旅游英语','论语智慧与人生发展','美术鉴赏','青少年心理卫生与问题儿童的诊断与治疗','趣味物理','日本人的审美意识','日本文化鉴赏','日本影视赏析','日常生活安全用电','社会保险学','社会调查研究','社会学基础','生活美学','生命科学与人类文明','生态旅游','拓展运动2','网络环境下的知识产权保护','卫生与健康','文学鉴赏','文学与人生','戏剧鉴赏','现当代散文与诗歌经典赏析','行政管理学','易经文化密码','音乐鉴赏','应用美学','营销策划','影视鉴赏','证券投资分析','证券投资学','中国旅游地理','中国文化概论','中外电影史','中西文化比较']
		
	# 通过循环筛选课程
	for i in range(len(all_cources_info_list)):
		a_cource = all_cources_info_list[i]
		cource_name = a_cource[0]
		cource_type = a_cource[1]
		cource_credit = a_cource[2]
		cource_score = a_cource[5]	
		# 判断课程是否及格
		if float(cource_score) < 60.0:
			un_pass_courses.append(a_cource)
			
		# 对及格的课程进行筛选
		else:
			if cource_type == '创新思维能力课程':
				if cource_name in base_need_cxswnlkc:
					learn_state = 1
					a_cource.append(learn_state)
					cxswnlkc.append(a_cource)
					credit_cxswnlkc += int(cource_credit)
				elif cource_name in base_need_xjcxswnlkc:
					learn_state = 1
					a_cource.append(learn_state)
					xjcxswnlkc.append(a_cource)
					credit_xjcxswnlkc += int(cource_credit)

				else:
					un_covered_courses.append(a_cource)							

			elif cource_type == '通识课程':
				if cource_name in base_need_tskc:
					learn_state = 1
					a_cource.append(learn_state)
					tskc.append(a_cource)
					credit_tskc += int(cource_credit)
				elif cource_name in base_need_tsxjb:
					learn_state = 1
					a_cource.append(learn_state)
					tsxjb.append(a_cource)
					credit_tsxjb += int(cource_credit)
				else:
					un_covered_courses.append(a_cource)			

			elif cource_type == '专业核心课程':
				if cource_name in base_need_zyhxkc:
					learn_state = 1
					a_cource.append(learn_state)
					zyhxkc.append(a_cource)
					credit_zyhxkc += int(cource_credit)
				else:
					un_covered_courses.append(a_cource)			
				
			elif cource_type == '应用创新课程':
				if cource_name in base_need_yycxkc:
					learn_state = 1
					a_cource.append(learn_state)
					yycxkc.append(a_cource)
					credit_yycxkc += int(cource_credit)
				else:
					un_covered_courses.append(a_cource)

			else:
				continue
	# 汇总信息，形成字典
	sort_cources_type_dict = {}
	sort_cources_type_dict['cxswnlkc'] = cxswnlkc
	sort_cources_type_dict['tskc'] = tskc
	sort_cources_type_dict['zyhxkc'] = zyhxkc
	sort_cources_type_dict['yycxkc'] = yycxkc
	sort_cources_type_dict['xjcxswnlkc'] = xjcxswnlkc
	sort_cources_type_dict['tsxjb'] = tsxjb

	sort_cources_credit_dict = {}
	sort_cources_credit_dict['credit_totle']=credit_yycxkc+credit_cxswnlkc+credit_tskc+credit_zyhxkc+credit_tsxjb+credit_xjcxswnlkc
	sort_cources_credit_dict['credit_cxswnlkc']=credit_cxswnlkc
	sort_cources_credit_dict['credit_tskc']=credit_tskc
	sort_cources_credit_dict['credit_zyhxkc']=credit_zyhxkc
	sort_cources_credit_dict['credit_yycxkc']=credit_yycxkc
	sort_cources_credit_dict['credit_tsxjb']=credit_tsxjb
	sort_cources_credit_dict['credit_xjcxswnlkc']=credit_xjcxswnlkc
	# 将结果处理为元祖，以便返回数据
	result = (sort_cources_credit_dict,sort_cources_type_dict,un_covered_courses,un_pass_courses)
	return result

# return_result(username_user,password_user)，此为主函数，传入用户和密码，依次调用登陆函数和信息获取函数，经过重组排序后返回一个元祖。
def return_result(username_user,password_user):
	logined_cookies = get_login_cookies(password_user,username_user)
	self_info_list = get_self_info(logined_cookies)
	all_cources_info_list = get_learned_cources(logined_cookies)
	result = get_analysis(all_cources_info_list)
	sort_cources_credit_dict,sort_cources_type_dict,un_covered_courses,un_pass_courses = result
	sort_cources_type_dict['cxswnlkc'] = sorted(sort_cources_type_dict['cxswnlkc'], key=operator.itemgetter(4))
	sort_cources_type_dict['tskc'] = sorted(sort_cources_type_dict['tskc'], key=operator.itemgetter(4))
	sort_cources_type_dict['zyhxkc'] = sorted(sort_cources_type_dict['zyhxkc'], key=operator.itemgetter(4)) 
	sort_cources_type_dict['yycxkc'] = sorted(sort_cources_type_dict['yycxkc'], key=operator.itemgetter(4))
	sort_cources_type_dict['xjcxswnlkc'] = sorted(sort_cources_type_dict['xjcxswnlkc'], key=operator.itemgetter(4))
	sort_cources_type_dict['tsxjb'] = sorted(sort_cources_type_dict['tsxjb'], key=operator.itemgetter(4))
	result = (self_info_list,all_cources_info_list,sort_cources_credit_dict,sort_cources_type_dict,un_covered_courses,un_pass_courses)
	return result